package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.IndicatorSystem;
import com.ruoyi.system.domain.QueryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Indicator;
import com.ruoyi.system.service.IIndicatorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-07-14
 */
@RestController
@RequestMapping("/system/indicator")
public class IndicatorController extends BaseController
{
    @Autowired
    private IIndicatorService indicatorService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:list')")
    @GetMapping("/list")
    public TableDataInfo list(Indicator indicator)
    {
        startPage();
        List<Indicator> list = indicatorService.selectIndicatorList(indicator);
        return getDataTable(list);
    }



    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Indicator indicator)
    {
        List<Indicator> list = indicatorService.selectIndicatorList(indicator);
        ExcelUtil<Indicator> util = new ExcelUtil<Indicator>(Indicator.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(indicatorService.selectIndicatorById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Indicator indicator)
    {
        return toAjax(indicatorService.insertIndicator(indicator));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Indicator indicator)
    {
        int x = indicatorService.updateIndicator(indicator);
        if(x == -1) return AjaxResult.error("该指标已在某指标体系中，无法修改！");
        return toAjax(indicatorService.updateIndicator(indicator));
    }

    /**
     * 批量删除指标（后端接口已实现）
     */
    @PreAuthorize("@ss.hasPermi('system:indicator:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int x = indicatorService.deleteIndicatorByIds(ids);
        if(x == -1) return AjaxResult.error("删除失败，可能原因如下：\n（1）待删除指标存在下级指标。\n（2）待删除指标已在某指标体系中。");
        else return AjaxResult.success("删除成功！");
    }


    @PreAuthorize("@ss.hasPermi('system:tree:list')")
    @GetMapping("/search")
    public TableDataInfo search(QueryVo queryVo)
    {
        System.out.println("HQY_DEBUG: "+queryVo);
        startPage();
        List<IndicatorSystem> list = indicatorService.search(queryVo);
        return getDataTable(list);
    }

    /**
     * 获得完整指标体系树形结构
     */
    @PreAuthorize("@ss.hasPermi('system:tree:query')")
    @GetMapping(value = "/indicatorSys/{id}")
    public TableDataInfo getIndicatorSys(@PathVariable("id") Long id)
    {
        return getDataTable(indicatorService.getIndicatorsBySystemId(id));
    }
    @PreAuthorize("@ss.hasPermi('system:tree:indicators')")
    @GetMapping(value = "/indicatorsTree")
    public TableDataInfo getIndicatorsInTree()
    {
        return getDataTable(indicatorService.getIndicatorListInTree());
    }

    @PreAuthorize("@ss.hasPermi('system:indicator:level')")
    @GetMapping(value = "/indicatorsTree/{id}/{level}")
    public TableDataInfo getIndicators(@PathVariable("id") Long id, @PathVariable("level") int level)
    {
        return getDataTable(indicatorService.getBasicIndicatorsBySystemId(id, level));
    }

    /**
     * 返回 Map<indicatorName,desc>
     */
    @GetMapping(value = "/getIndicatorName_DescMap")
    public AjaxResult getIndicatorName_DescMap(){
        return AjaxResult.success(indicatorService.getIndicatorName_DescMap());
    }

}
